const { DataTypes, Sequelize } = require("sequelize");
const sequelize = require("./sequelizeBaseConfig.js");

// 文章模型
const Article = sequelize.define(
  "Article",
  {
    id: {
      type: DataTypes.BIGINT,
      comment: "ID",
      autoIncrement: true, //自增
      primaryKey: true, //主键
    },
    created_at: {
      type: DataTypes.DATE,
      comment: "创建时间",
      defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"), //如果没有设置该字段的值则使用默认
    },
    updated_at: {
      type: DataTypes.DATE,
      comment: "更新时间",
      defaultValue: Sequelize.literal(
        "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
      ),
    },
    category_id: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: "分类 ID",
      defaultValue: 1,
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "用户 ID",
    },
    title: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: "文章标题",
    },
    desc: {
      type: DataTypes.STRING(200),
      comment: "文章描述",
      allowNull: true,
    },
    content: {
      type: DataTypes.TEXT,
      comment: "文章内容",
      allowNull: true,
    },
    img: {
      type: DataTypes.STRING(100),
      comment: "封面图片地址",
      allowNull: false,
    },
    type: {
      type: DataTypes.TINYINT,
      comment: "类型(1-原创 2-转载 3-翻译)",
    },
    status: {
      type: DataTypes.TINYINT,
      comment: "状态(1-公开 2-私密)",
    },
    is_top: {
      type: DataTypes.TINYINT,
      allowNull: true,
      defaultValue: 0,
      comment: "是否置顶(0-否 1-是)",
    },
    is_delete: {
      type: DataTypes.TINYINT,
      allowNull: true,
      defaultValue: 0,
      comment: "是否放到回收站(0-否 1-是)",
    },
    original_url: {
      type: DataTypes.STRING(100),
      comment: "源链接",
      allowNull: true,
    },
  },
  {
    tableName: "article",
    timestamps: false,
  }
);

// 文章-标签 关联模型
const ArticleTag = sequelize.define(
  "ArticleTag",
  {
    article_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
    },
    tag_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
    },
  },
  {
    tableName: "article_tag",
    timestamps: false,
  }
);

module.exports = {
  Article,
  ArticleTag,
};
